package com.yt.lxh.acl.controller;

import com.yt.lxh.acl.service.AdminService;
import com.yt.lxh.acl.service.MenuService;
import com.yt.lxh.acl.service.ValidateCodeService;
import com.yt.lxh.common.result.Result;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.common.utils.AuthContextUtil;
import com.yt.lxh.common.utils.JwtUtils;
import com.yt.lxh.dto.system.LoginDto;
import com.yt.lxh.vo.system.LoginVo;
import com.yt.lxh.vo.menu.MenuVo;
import com.yt.lxh.vo.system.ValidateCodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "登录接口")
@RestController
@RequestMapping("/admin/acl")
//@CrossOrigin
public class LoginController {
    @Autowired
    private AdminService adminService;

    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private MenuService menuService;


    @ApiOperation("查询用户可以操作菜单")
    @GetMapping(value = "/menus")
    public Result menus(){
        List<MenuVo> list = menuService.findMenusByUserId();
        return Result.success(AuthContextUtil.get());
    }

    @ApiOperation("获取当前登录用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(){
        return Result.success(AuthContextUtil.get());
    }

    @ApiOperation("管理员登录")
    @PostMapping("login")
    public Result login(@RequestBody LoginDto loginDto){
        System.out.println(loginDto);
        LoginVo loginVo = adminService.login(loginDto);
        return Result.success(loginVo);
    }

    @ApiOperation("管理员登出")
    @PostMapping("logout")
    public Result logout(@RequestHeader(name = "token") String token){
        adminService.logout(token);
        return Result.success(null);
    }

    @ApiOperation("生成图片验证码")
    @GetMapping("generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation("token返回")
    @GetMapping("/session/{token}")
    public Result login(@PathVariable String token){
        Integer adminId = JwtUtils.verifyAdminTokenAndGetId(token);
        return Result.success(adminId);
    }
}
